package jee.boot.module.mail.sender;

import jee.boot.common.utils.StringUtils;
import jee.boot.entity.mail.Mail;
import jee.boot.module.mail.interceptor.MailInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedList;

/**
 * 邮件发送
 */
//@Component
public class DefaultMailTaskHandler implements MailTaskHandler {
    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());
    private LinkedList<MailInterceptor> preProcessingList = new LinkedList<>();// 前置处理队列
    private LinkedList<MailInterceptor> postProcessingList = new LinkedList<>();// 后置处理队列
    private DefaultMailSender sender;//邮件发送接口

    public final DefaultMailTaskHandler addPreInterceptor(final MailInterceptor interceptor) {
        preProcessingList.add(interceptor);
        return this;
    }

    public final DefaultMailTaskHandler addPostInterceptor(final MailInterceptor interceptor) {
        postProcessingList.add(interceptor);
        return this;
    }

    public LinkedList<MailInterceptor> getPreProcessingList() {
        return preProcessingList;
    }

    public void setPreProcessingList(LinkedList<MailInterceptor> preProcessingList) {
        this.preProcessingList = preProcessingList;
    }

    public LinkedList<MailInterceptor> getPostProcessingList() {
        return postProcessingList;
    }

    public void setPostProcessingList(LinkedList<MailInterceptor> postProcessingList) {
        this.postProcessingList = postProcessingList;
    }

    public DefaultMailSender getSender() {
        return sender;
    }

    public void setSender(DefaultMailSender sender) {
        this.sender = sender;
    }

    /**
     * 邮件发送
     *
     * @param mail
     */
    @Override
    public void send(Mail mail) {

        if (preProcessingList != null && !preProcessingList.isEmpty()) {
            for (MailInterceptor pre : preProcessingList) {
                try {
                    pre.process(mail);
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("发送邮件前置处理失败 :" + e.getMessage());
                }
            }
        }
        try {
            sender.send(mail);
            mail.setSendStatus("Y");// 发送成功
            mail.setMessage("发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发送邮件失败 :" + e.getMessage());
            mail.setSendStatus("N");// 发送失败
            mail.setMessage("发送失败"+e.getMessage());
        }
        if (postProcessingList != null && !postProcessingList.isEmpty()) {
            for (MailInterceptor post : postProcessingList) {
                try {
                    post.process(mail);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (StringUtils.equals("Y", mail.getSendStatus())) {
                        mail.setSendStatus("W");// 发送异常(短信发送成功,但后置处理异常)
                        logger.error("发送邮件后置处理异常(邮件发送成功,但后置处理异常) :" + e.getMessage());
                        mail.setMessage("发送异常");
                    }
                    mail.setMessage(mail.getMessage()+e.getMessage());
                }
            }
        }
    }

    /**
     * 发送日历类邮件
     * @param mail 邮件信息
     */
    @Override
    public void sendCalendarMail(Mail mail) {
        try {
            sender.sendCalendarMail(mail);
            mail.setSendStatus("Y");// 发送成功
            mail.setMessage("发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发送邮件失败 :" + e.getMessage());
            mail.setSendStatus("N");// 发送失败
            mail.setMessage("发送失败"+e.getMessage());
        }
    }
}
